package com.health.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.health.R;
import com.health.model.BD_DD_INFO_Checked;
import com.health.tablebean.BD_DD_INFO;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;
import com.health.widget.EditTextWithDate;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;

/**
 * 单选
 * 
 * @author Administrator
 *
 */
public class RadioButtonAdapter extends BaseAdapter implements OnCheckedChangeListener {
	private RBACheckedChangeListener rbaccl;
	public List<BD_DD_INFO_Checked> list_check = new ArrayList<BD_DD_INFO_Checked>();
	private BD_DD_INFO_Checked tempGridViewItem;
	private LayoutInflater layoutInflater;
	private EditTextWithDate editTextWithDate;
	private EditText otherEdit;
	private NiceSpinner spinner;
	/** 选择无:禁止编辑 选择有:可以编辑 */
	private List<EditText> mlstEt;
	private boolean disable = false;
	private boolean otherVisible = true;
	private boolean cCheck = true;
	/** 患者两次随访患病情况是否选择无 **/
	private boolean lcsfhbIsCheckNo = true;
	private List<RadioButton> rbs = new ArrayList<RadioButton>();
	private List<RadioButtonViewHolder> lstvh = new ArrayList<RadioButtonViewHolder>();
	private String[] otherVis = new String[] { "有", "异常", "已戒酒", "未恢复", "是", "其他", "其他遗传代谢病" };// 显示其他文本框的选项
	private String[] otherInVis = new String[] { "无", "未见异常", "未戒酒", "已恢复" };// 隐藏其他文本框的选项
	private boolean hasOtherEditText;// 是否含有其他的EditText
	private static int iotherMaxLength = -1;

	public void setCheckedChengeLinstener(RBACheckedChangeListener rbaccl) {
		this.rbaccl = rbaccl;
	}

	/**
	 * RadioButtonAdapter RadioButtons's Listener
	 * 
	 * @author Administrator
	 *
	 */
	public static interface RBACheckedChangeListener {
		void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
	}

	public void setOtherMaxLength(int maxLength) {
		this.iotherMaxLength = maxLength;
	}

	/**
	 * 是否打开Other
	 * 
	 * @author 鹿云龙
	 * @date 2016年5月6日 上午11:00:47
	 */
	private boolean otherIsVis(String targetValue) {
		return Arrays.asList(otherVis).contains(targetValue);
	}

	/**
	 * 是否关闭Other
	 * 
	 * @author 鹿云龙
	 * @date 2016年5月6日 上午11:00:47
	 */
	private boolean otherIsInVis(String targetValue) {
		return Arrays.asList(otherInVis).contains(targetValue);
	}

	public RadioButtonAdapter(Context context, List<BD_DD_INFO_Checked> list_check) {
		this.list_check = list_check;
		this.layoutInflater = LayoutInflater.from(context);
	}

	/**
	 * @param list_bd
	 * @param context
	 */
	public RadioButtonAdapter(List<BD_DD_INFO> list_bd, Context context) {
		super();
		this.list_check = getBD_DD_INFORCheckeds(list_bd);
		layoutInflater = LayoutInflater.from(context);
	}

	public RadioButtonAdapter(List<BD_DD_INFO> list_bd, Context context, NiceSpinner spinner) {
		super();
		this.list_check = getBD_DD_INFORCheckeds(list_bd);
		this.layoutInflater = LayoutInflater.from(context);
		this.spinner = spinner;
	}

	public RadioButtonAdapter(List<BD_DD_INFO> list_bd, Context context, EditText et) {
		super();
		this.list_check = getBD_DD_INFORCheckeds(list_bd);
		this.layoutInflater = LayoutInflater.from(context);
		this.otherEdit = et;
		if (otherEdit != null) {
			// GlobalConts.setMaxLength(iotherMaxLength, otherEdit);
			otherEdit.setText("");
			// otherEdit.setEnabled(false);
			// otherEdit.setFocusable(false);
			// otherEdit.setFocusableInTouchMode(false);
			// otherEdit.setClickable(false);
		}
		// TODO
	}

	public RadioButtonAdapter(List<BD_DD_INFO> list_bd, Context context, EditText et, EditText... editTexts) {
		super();
		this.edits = editTexts;
		this.list_check = getBD_DD_INFORCheckeds(list_bd);
		this.layoutInflater = LayoutInflater.from(context);
		this.otherEdit = et;
		if (otherEdit != null) {
			if (iotherMaxLength != -1) {
				GlobalConts.setMaxLength(iotherMaxLength, otherEdit);
			}
			otherEdit.setText("");
			otherEdit.setEnabled(false);
			otherEdit.setFocusable(false);
			otherEdit.setFocusableInTouchMode(false);
			otherEdit.setClickable(false);
		}
		if (edits != null) {
			for (EditText ets : edits) {
				ets.setText("false");
				ets.setEnabled(false);
			}
		}
		// TODO
	}

	public RadioButtonAdapter(List<BD_DD_INFO> list_bd, Context context, List<EditText> et) {
		super();
		this.list_check = getBD_DD_INFORCheckeds(list_bd);
		this.layoutInflater = LayoutInflater.from(context);
		this.mlstEt = et;
		for (int i = 0; i < mlstEt.size(); i++) {
			mlstEt.get(i).setEnabled(false);
			mlstEt.get(i).setFocusable(false);
			mlstEt.get(i).setFocusableInTouchMode(false);
			mlstEt.get(i).setClickable(false);
		}
	}
	
	public RadioButtonAdapter(List<BD_DD_INFO> list_bd, Context context, List<EditText> et, boolean isEnable) {
		super();
		this.list_check = getBD_DD_INFORCheckeds(list_bd);
		this.layoutInflater = LayoutInflater.from(context);
		this.mlstEt = et;
		if (!isEnable) {
			for (int i = 0; i < mlstEt.size(); i++) {
				mlstEt.get(i).setEnabled(false);
				mlstEt.get(i).setFocusable(false);
				mlstEt.get(i).setFocusableInTouchMode(false);
				mlstEt.get(i).setClickable(false);
			}
		}
	}

	/**
	 * 时间文本框
	 * 
	 * @param list_bd
	 * @param context
	 */
	public RadioButtonAdapter(List<BD_DD_INFO> list_bd, Context context, EditTextWithDate et) {
		super();
		this.list_check = getBD_DD_INFORCheckeds(list_bd);
		layoutInflater = LayoutInflater.from(context);
		editTextWithDate = et;
	}

	/**
	 * 清空选中项
	 * 
	 * @author 鹿云龙
	 * @date 2015年12月24日 下午2:45:06
	 */
	public void clerChecked() {
		for (RadioButton bdcheck : rbs) {
			bdcheck.setChecked(false);
		}
	}

	/**
	 * 数据总数
	 */
	@Override
	public int getCount() {
		return list_check.size();
	}

	/**
	 * 获取当前数据
	 */
	@Override
	public Object getItem(int position) {
		return list_check.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		RadioButtonViewHolder avh = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.radiobuttonitem, null);
			avh = new RadioButtonViewHolder();
			avh.rbButton = (RadioButton) convertView.findViewById(R.id.radiobutton);
			avh.otherText = (EditText) convertView.findViewById(R.id.othertext);
			rbs.add(avh.rbButton);
			convertView.setTag(R.id.TAG_VIEWHOLDER, avh);
		} else {
			avh = (RadioButtonViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
		}
		lstvh.add(avh);
		setData(avh, position);
		return convertView;
	}

	/**
	 * 获取选中项的主键id
	 * 
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月22日 上午11:58:46
	 */
	public String getChecked() {
		String str = "";
		for (BD_DD_INFO_Checked bdic : list_check) {
			if (bdic.getIsChecked()) {
				str = bdic.getBd_info().getID();
			}
		}
		return str;
	}

	/**
	 * 获取选中项的Other
	 * 
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月22日 上午11:58:46
	 */
	public String getCheckedOther() {

		if (otherEdit != null) {
			String sssss = otherEdit.getText().toString();
			if (sssss.length() > 0) {
				System.out.println(sssss);
			}
			return otherEdit.getText().toString();
		} else {
			String str = "";
			for (BD_DD_INFO_Checked bdic : list_check) {
				if (bdic.getIsChecked()) {
					str = bdic.getContent();
				}
			}
			return str;
		}
	}

	/**
	 * 获取选中项的对象
	 * 
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月22日 上午11:58:46
	 */
	public BD_DD_INFO getCheckedBD() {
		BD_DD_INFO bdi = new BD_DD_INFO();
		for (BD_DD_INFO_Checked bdic : list_check) {
			if (bdic.getIsChecked()) {
				return bdic.getBd_info();
			}
		}
		return bdi;
	}

	/**
	 * 获取位置 0,1
	 * 
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月24日 下午1:44:50
	 */
	public int getIndex(String strCheckedBDId) {
		int index = -1;
		for (int i = 0; i < list_check.size(); i++) {
			if (list_check.get(i).getBd_info().getID().equals(strCheckedBDId)) {
				return i;
			}
		}
		return index;
	}

	/**
	 * 设置选中项id 和其他数据
	 * 
	 * @param id
	 *            主键id
	 * @author 鹿云龙
	 * @date 2015年12月22日 下午12:00:45
	 */
	public void setChecked(String id, String Other) {

		for (BD_DD_INFO_Checked bdcheck : list_check) {
			String str_sjxtId_1 = bdcheck.getBd_info().getID();
			Log.w("dddddddddddd", str_sjxtId_1);
			if (str_sjxtId_1.equals(id)) {
				bdcheck.setIsChecked(true);
				bdcheck.setContent(Other);
				if (otherIsVis(bdcheck.getBd_info().getDATAINFO())) {
					if (otherEdit != null) {
						otherTextViewEditor(true);
						if (iotherMaxLength != -1) {
							GlobalConts.setMaxLength(iotherMaxLength, otherEdit);
						}
						otherEdit.setText(Other);
					}
					if (mlstEt != null) {
						for (int i = 0; i < mlstEt.size(); i++) {
							mlstEt.get(i).setEnabled(true);
							mlstEt.get(i).setFocusable(true);
							mlstEt.get(i).setFocusableInTouchMode(true);
							mlstEt.get(i).setClickable(true);
						}
					}
				}

				break;
			}
		}

	}

	/**
	 * 禁止选中单选按钮
	 * 
	 * @author 鹿云龙
	 * @date 2015年12月22日 下午3:05:00
	 */
	public void checkedClose() {
		for (BD_DD_INFO_Checked bdcheck : list_check) {
			bdcheck.setIsChecked(false);
		}
		for (RadioButton rb : rbs) {
			rb.setChecked(false);
		}
		disable = true;
	}

	/**
	 * 启用单选按钮
	 * 
	 * @author 鹿云龙
	 * @date 2015年12月22日 下午4:14:53
	 */
	public void checkOpen() {
		disable = false;
	}

	/**
	 * 设置选中项id
	 * 
	 * @param id
	 *            主键id
	 * @author 鹿云龙
	 * @date 2015年12月22日 下午12:00:45
	 */
	public void setChecked(String id) {
		if (otherEdit != null) {
			if (TextUtils.isEmpty(id)) {
				otherTextViewEditor(false);
				return;
			}
		}
		for (BD_DD_INFO_Checked bdcheck : list_check) {
			String str_sjxtId_1 = bdcheck.getBd_info().getID();
			if (str_sjxtId_1.equals(id)) {
				bdcheck.setIsChecked(true);// TODO
				Log.e("tag", str_sjxtId_1 + "++++++++++++++++++");
				break;
			}
		}
	}

	public void canCheck(boolean cCheck) {
		this.cCheck = cCheck;
		if (cCheck) {
			for (BD_DD_INFO_Checked bdcheck : list_check) {
				bdcheck.setIsChecked(false);
				bdcheck.setContent("");
			}
			clerChecked();

			if (otherVisible) {
				if (otherEdit != null) {
					otherEdit.setText("");
					otherEdit.setEnabled(true);
				}
			}
		} else {
			if (otherVisible) {
				if (otherEdit != null) {
					otherEdit.setText("");
					otherEdit.setEnabled(false);
				}
			}
		}

	}

	/**
	 * 设置是否显示其他文本框，默认为true
	 * 
	 * @author 鹿云龙
	 * @date 2015年12月22日 下午4:19:33
	 */
	public void setOtherTextVisible(boolean visible) {
		otherVisible = visible;
	}

	private void setData(RadioButtonViewHolder h, int position) {

		// 获取自定义的类实例
		tempGridViewItem = list_check.get(position);
		h.rbButton.setOnCheckedChangeListener(null);
		h.rbButton.setChecked(tempGridViewItem.getIsChecked());

		h.rbButton.setText(tempGridViewItem.getBd_info().getDATAINFO());
		h.otherText.setText(tempGridViewItem.getContent());
		String str_datainfo = list_check.get(position).getBd_info().getDATAINFO();

		if (otherIsVis(str_datainfo)) {// 是否显示单选按钮后的文本框
			if (otherVisible) {
				otherEdit = h.otherText;// 暂时设计为一个其他文本框，后期添加文本框和选择按钮文字映射
				if (iotherMaxLength != -1) {
					GlobalConts.setMaxLength(iotherMaxLength, otherEdit);
					GlobalConts.setMaxLength(iotherMaxLength, h.otherText);
				}
				// h.otherText.setVisibility(View.VISIBLE);
				// h.otherText.setEnabled(true);
				// h.otherText.addTextChangedListener(new
				// OnTextChangedListener(tempGridViewItem));
				otherEdit.setVisibility(View.VISIBLE);
				otherEdit.setEnabled(true);
				otherEdit.addTextChangedListener(new OnTextChangedListener(tempGridViewItem));
				otherEditTextEditor(false);
			}
			if (edits != null) {
				for (EditText et : edits) {
					et.setEnabled(true);
				}
			}
		} else {
			// otherEdit = h.otherText;// 暂时设计为一个其他文本框，后期添加文本框和选择按钮文字映射
			// h.otherText.setEnabled(false);
			otherEditTextEditor(false);
		}
		if (editTextWithDate != null) {
			if (str_datainfo.equals("不详") && tempGridViewItem.getIsChecked()) {
				editTextWithDate.showDialog(false);
			}
		}
		otherEditTextEditor(false);
		if (otherVisible) {
			if (tempGridViewItem.getIsChecked()) {
				if (otherIsInVis(str_datainfo)) {
					otherEditTextEditor(false);
				}
			}
		}
		if (otherEdit != null) {
			if (tempGridViewItem.getIsChecked()) {
				if (otherIsInVis(str_datainfo)) {
					otherTextViewEditor(false);
				}
			} else {
				if (otherIsVis(str_datainfo)) {
					otherTextViewEditor(false);
				}
			}
		}

		h.rbButton.setOnCheckedChangeListener(this);

		h.rbButton.setTag(R.id.TAG, tempGridViewItem);
		tempGridViewItem.setRbv(h);
	}

	public final class RadioButtonViewHolder {
		RadioButton rbButton;
		View choseView;
		EditText otherText;
	}

	private EditText edits[];

	private CheckedChange cc;

	class CheckedChange {

	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO
		BD_DD_INFO_Checked check = (BD_DD_INFO_Checked) buttonView.getTag(R.id.TAG);
		if (rbaccl != null) {
			rbaccl.onCheckedChanged(buttonView, isChecked);
		}
		if (!cCheck) {
			for (BD_DD_INFO_Checked bdcheck : list_check) {
				bdcheck.setIsChecked(false);
				bdcheck.setContent("");
			}
			clerChecked();
			return;
		}

		for (BD_DD_INFO_Checked bdcheck : list_check) {
			bdcheck.setIsChecked(false);
			bdcheck.setContent("");
		}
		check.setIsChecked(isChecked);
		if (disable) {
			check.setIsChecked(false);
		}

		if (spinner != null) {
			if ("无".equals(check.getBd_info().getDATAINFO())) {
				spinner.setClickable(false);
				spinner.setEnabled(false);

			} else {
				spinner.setClickable(true);
				spinner.setEnabled(true);

			}
		}
		if (editTextWithDate != null) {
			if (check.getBd_info().getDATAINFO().equals("明确")) {
				editTextWithDate.setFocusable(true);
				editTextWithDate.setFocusableInTouchMode(true);
				editTextWithDate.setClickable(true);
				((EditTextWithDate) editTextWithDate).showDialog(true);
			} else if (check.getBd_info().getDATAINFO().equals("不详")) {
				editTextWithDate.setText("");
				editTextWithDate.setFocusable(false);
				editTextWithDate.setFocusableInTouchMode(false);
				editTextWithDate.setClickable(false);
				((EditTextWithDate) editTextWithDate).showDialog(false);
			}
		}

		if (otherEdit != null) {
			if (otherIsVis(check.getBd_info().getDATAINFO())) {
				otherTextViewEditor(true);
			} else if (otherIsInVis(check.getBd_info().getDATAINFO())) {
				otherTextViewEditor(false);
			}
		}
		if (otherIsVis(check.getBd_info().getDATAINFO())) {
			if (otherVisible) {
				check.getRbv().otherText.setFocusable(true);
				check.getRbv().otherText.setEnabled(true);
				check.getRbv().otherText.setFocusableInTouchMode(true);
				check.getRbv().otherText.setClickable(true);
			}
		} else if (otherIsInVis(check.getBd_info().getDATAINFO())) {
			check.getRbv().otherText.setText("");
			check.getRbv().otherText.setFocusable(false);
			check.getRbv().otherText.setFocusableInTouchMode(false);
			check.getRbv().otherText.setClickable(false);
		}
		if (mlstEt != null) {
			if (otherIsVis(check.getBd_info().getDATAINFO())) {
				for (int i = 0; i < mlstEt.size(); i++) {
					mlstEt.get(i).setFocusable(true);
					mlstEt.get(i).setEnabled(true);
					mlstEt.get(i).setFocusableInTouchMode(true);
					mlstEt.get(i).setClickable(true);
				}
			} else if (otherIsInVis(check.getBd_info().getDATAINFO())) {
				for (int i = 0; i < mlstEt.size(); i++) {
					mlstEt.get(i).setText("");
					mlstEt.get(i).setFocusable(false);
					mlstEt.get(i).setFocusableInTouchMode(false);
					mlstEt.get(i).setClickable(false);
				}
			}
		}
		notifyDataSetChanged();
	}

	private class OnTextChangedListener implements TextWatcher {
		private BD_DD_INFO_Checked w;

		public OnTextChangedListener(BD_DD_INFO_Checked w) {
			super();
			this.w = w;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			String str_content = s.toString();
			w.setContent(str_content);
		}

	}

	/**
	 * 获取BD_DD_INFO s的BD_DD_INFO_Checked s
	 * 
	 * @param listZZs
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月18日 下午3:38:40
	 */
	public static List<BD_DD_INFO_Checked> getBD_DD_INFORCheckeds(List<BD_DD_INFO> listZZs) {
		List<BD_DD_INFO_Checked> list_check = new ArrayList<BD_DD_INFO_Checked>();
		int size = listZZs.size();
		if (size > 0) {
			for (int i = 0; i < size; i++) {
				BD_DD_INFO_Checked check = new BD_DD_INFO_Checked();
				check.setIsChecked(false);
				check.setBd_info(listZZs.get(i));
				check.setContent("");
				list_check.add(check);
			}
		}
		return list_check;
	}

	/**
	 * 设置其他输入框是否可编辑
	 * 
	 * @param editor
	 *            if(editor){可编辑}
	 * @author 鹿云龙
	 * @date 2016年5月6日 上午9:56:51
	 */
	private void otherTextViewEditor(boolean editor) {
		if (!editor) {
			otherEdit.setText("");
			otherEdit.setEnabled(false);
			otherEdit.setFocusable(false);
			otherEdit.setFocusableInTouchMode(false);
			otherEdit.setClickable(false);
			if (null != edits) {
				for (EditText et : edits) {
					et.setText("");
					et.setEnabled(false);
				}
			}
		} else {
			otherEdit.setFocusable(true);
			otherEdit.setEnabled(true);
			otherEdit.setFocusableInTouchMode(true);
			otherEdit.setClickable(true);
			if (iotherMaxLength != -1) {
				GlobalConts.setMaxLength(iotherMaxLength, otherEdit);
			}
			if (null != edits) {
				for (EditText et : edits) {
					// et.setText("");
					et.setEnabled(true);
				}
			}
		}
	}

	private void otherEditTextEditor(boolean editor) {

	}
}